// https : // iai.sh.cn/problem/610
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> f[i];
  for (int i = 1; i <= n; i++) {
    if (f[i] == i) {
      a[i] = i;
      continue;
    }
    if (!flag[i]) {
      flag[i] = true;
      cnt = 0;
      v.clear();
      memset(m, 0, sizeof(m));
      v.push_back(i);
      int idx = f[i];
      while (idx != i) {
        cnt++;
        m[idx] = cnt;
        value[cnt] = idx;
        v.push_back(idx);
        flag[idx] = true;
        idx = f[idx];
      }
      m[i] = ++cnt;
      value[cnt] = i;
      int move = k % cnt;
      for (int j = 0; j < v.size(); j++) {
        int now = m[v[j]];
        now = (now + move) % cnt;
        if (now == 0)
          now = cnt;
        a[value[now]] = v[j];
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cout << a[i] << ' ';
  return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5 + 10;
int f[maxn]; // 规则
int a[maxn]; // 存储最后的每个位置对应的牌的编号
bool flag[maxn];
vector<int> v;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> f[i]; // i位置的下一个位置是 f[i];
  }
  for (int i = 1; i <= n; i++) {
    if (flag[i])
      continue;
    v.clear();
    // 创建环
    int m = i;
    v.push_back(m);
    flag[m] = true;
    while (f[m] != i) {
      m = f[m];
      flag[m] = true;
      v.push_back(m);
    }
    // 处理环
    int t = v.size(); // 循环节的长度
    for (int p = 0; p < t; p++) {
      int x = v[p];
      int p1 = (p + k) % t;
      int y = v[p1];
      a[y] = x; // 最后y位置的牌的编号是x
    }
  }
  for (int i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }

  return 0;
}